package com.example.tbag.healthcare.Activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.tbag.healthcare.Adapter.HealthListAdapter;
import com.example.tbag.healthcare.Model.DragInfo;
import com.example.tbag.healthcare.Model.HumanInfo;
import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Tbag on 2017/4/21.
 */

public class HealthActivity extends AppCompatActivity {
    private List<DragInfo> dragInfos = new ArrayList<>();
    private List<HumanInfo> humanInfos = new ArrayList<>();
    private HealthListAdapter adapter;
    @BindView(R.id.list)
    ListView listView;
    private int id;
    private DatabaseHelper helper;

    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }
    @OnClick(R.id.btn_back)
    public void back() {
        this.finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id",0);
        helper = new DatabaseHelper(this);
        getPersons();
        getPersons1();
        initList();
    }

    private void initList() {
        adapter = new HealthListAdapter(humanInfos,dragInfos,this);
        listView.setAdapter(adapter);
    }


    public void getPersons() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("drag",null,"id =?",new String[]{id+""},null,null,null);
        if (cursor != null && cursor.moveToFirst()) {
            dragInfos.add(getDragInfor(cursor));
            while (cursor.moveToNext()) {
                dragInfos.add(getDragInfor(cursor));
            }
            cursor.close();
            db.close();
        }
    }

    public DragInfo getDragInfor(Cursor cursor) {
        String time = cursor.getString(cursor.getColumnIndex("time"));
        String drag = cursor.getString(cursor.getColumnIndex("drag"));
        String count = cursor.getString(cursor.getColumnIndex("count"));
        String people = cursor.getString(cursor.getColumnIndex("done"));
        int id = cursor.getInt(cursor.getColumnIndex("_id"));
        String price = cursor.getString(cursor.getColumnIndex("price"));
        String cure = cursor.getString(cursor.getColumnIndex("cure"));
        DragInfo dragInfo = new DragInfo(id,time, cure,drag,price, count, people);
        return dragInfo;
    }

    public void getPersons1() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("human",null,"id =?",new String[]{id+""},null,null,null);
        if (cursor != null && cursor.moveToFirst()) {
            humanInfos.add(getHumanInfo(cursor));
            while (cursor.moveToNext()) {
                humanInfos.add(getHumanInfo(cursor));
            }
            cursor.close();
            db.close();
        }
    }

    public HumanInfo getHumanInfo(Cursor cursor) {
        int id1 = cursor.getInt(cursor.getColumnIndex("_id"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String time = cursor.getString(cursor.getColumnIndex("time"));
        String temper = cursor.getString(cursor.getColumnIndex("temper"));
        String pulse = cursor.getString(cursor.getColumnIndex("pulse"));
        String breath = cursor.getString(cursor.getColumnIndex("breath"));
        String  blood = cursor.getString(cursor.getColumnIndex("blood"));
        String sugar = cursor.getString(cursor.getColumnIndex("sugar"));
        String eg = cursor.getString(cursor.getColumnIndex("eg"));
        HumanInfo humanInfo = new HumanInfo(id1,date, time, temper, pulse, breath, blood,sugar,eg);
        return humanInfo;
    }

}
